package com.xxh.kfc.service.impl;

import com.xxh.kfc.dao.FoodDao;
import com.xxh.kfc.dao.OrderdetailDao;
import com.xxh.kfc.dao.OrdersDao;
import com.xxh.kfc.entity.Food;
import com.xxh.kfc.entity.Orderdetail;
import com.xxh.kfc.entity.Orders;
import com.xxh.kfc.entity.vo.OrderDetailVo;
import com.xxh.kfc.service.OrderDetailService;
import com.xxh.kfc.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;
import java.util.List;

public class OrderDetailServiceImpl implements OrderDetailService {




    //根据订单ID获取订单详情
    @Override
    public List<OrderDetailVo>getDetailsByOid(int oid){
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        //引入dao
        OrdersDao ordersDao = sqlSession.getMapper(OrdersDao.class);
        OrderdetailDao orderdetailDao = sqlSession.getMapper(OrderdetailDao.class);
        FoodDao foodDao = sqlSession.getMapper(FoodDao.class);


        //原始的订单列表
        List<Orderdetail> rawDetailsList = orderdetailDao.getDetailsByOid(oid);
        Orders order = ordersDao.selectByPrimaryKey(oid);

        List<OrderDetailVo> list=new ArrayList<>();
        //丰富当前订单详情
        for(Orderdetail orderdetail:rawDetailsList){
            OrderDetailVo detailVo = new OrderDetailVo();
            Food food = foodDao.selectByPrimaryKey(orderdetail.getFid());
            detailVo.setFoodName(food.getFoodname());
            detailVo.setFoodPrice(food.getPrice());
            detailVo.setFid(food.getId());
            detailVo.setOrderNum(orderdetail.getOrdnum());
            detailVo.setOrdertime(order.getOrdertime());
            list.add(detailVo);
        }
        sqlSession.close();
        return list;
    }
}
